home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr46 / multi_ui.zip / DEMO.CPP < prev    next >
C/C++ Source or Header  |  1993-04-16  |  7KB  |  505 lines

  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdarg.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <ctype.h>
  7. #include "multi_ui.h"
  8. #include "views.h"
  9.  
  10. long COLORS[]={
  11. BLACK,BLUE,GREEN,CYAN,RED,MAGENTA,BROWN,LIGHTGRAY,DARKGREY,LIGHTBLUE,    
  12. LIGHTGREEN,LIGHTCYAN,LIGHTRED,LIGHTMAGENTA,YELLOW,WHITE};        
  13.  
  14.  
  15. MENU *file,*edit,*sub;
  16.  
  17. class M_FILE:public MENU
  18. {
  19. public:
  20.     M_FILE();
  21.     void event(int i);
  22. };
  23.  
  24.  
  25. class M_EDIT:public MENU
  26. {
  27. public:
  28.     M_EDIT();
  29.     void event(int i);
  30. };
  31.  
  32.  
  33. class M_SUBMENU:public MENU
  34. {
  35. public:
  36.     M_SUBMENU();
  37.     void event(int i);
  38. };
  39.  
  40.  
  41. void alp_failed(void){}
  42. void update_menus(void){}
  43. void idle_function(void){}
  44.  
  45.  
  46.  
  47.  
  48. M_FILE::M_FILE(void)
  49. {        
  50.     create(128,"File");
  51.     add('N',"New Graphic",NULL);
  52.     add('G',"View a GIF",NULL);
  53.     add('O',"Sub Menus",sub);
  54.     add(000,"-----",NULL);
  55.     add('P',"Print...",NULL);
  56.     add(000,"-----",NULL);
  57.     add('Q',"Quit",NULL);
  58. }
  59.  
  60.  
  61. void M_FILE::event(int i)
  62. {
  63. WINDOW *w;
  64.     
  65.     switch(i)
  66.         {
  67.         case 1:
  68.             {
  69.             WINDOW *w;
  70.             V_BITMAP *bm;
  71.             char name[80];
  72.  
  73.               w=new WINDOW;
  74.             bm=new V_BITMAP;
  75.  
  76.             w->create(128);
  77.             app->add(w);
  78.             w->add(bm);
  79.             w->set_title("Untitled");
  80.             }
  81.         break;
  82.  
  83.         case 2:
  84.             {
  85.             WINDOW *w;
  86.             V_GIF *gif;
  87.             char name[80];
  88.             FILEIO *fp;
  89.  
  90.  
  91.             *name=0;
  92.             fp=new FILEIO;
  93.             fp->open_file("",name,'r',"","");
  94.             if(!fp->error())
  95.                 {
  96.                 w=new WINDOW;
  97.                 gif=new V_GIF;
  98.  
  99.                 w->create(128);
  100.                 gif->cV_GIF(0,0,0,0,0);
  101.                 app->add(w);
  102.                 w->add(gif);
  103.                 gif->gif_in(fp);
  104.                 w->set_title(name);
  105.  
  106.                 w->update();
  107.                 w->resize(gif->maxx,gif->maxy);
  108.                 w->redraw();
  109.                 }
  110.             delete fp;
  111.             }
  112.         break;
  113.  
  114.         case 5:
  115.             w=app->front_window();
  116.             if(w==NULL)
  117.                 {
  118.                 beep();
  119.                 return;
  120.                 }
  121.             w->print();
  122.             break;
  123.                     
  124.         case 7:
  125.             global_quit++;
  126.             break;
  127.         }
  128. }
  129.     
  130.         
  131. M_EDIT::M_EDIT(void)
  132. {
  133.  
  134.     create(129,"Edit");
  135.     add('X',"Cut",0);
  136.     add('C',"Copy",0);
  137.     add('V',"Paste",0);
  138.  
  139.  
  140. }
  141.  
  142.  
  143. #pragma argsused
  144. void M_EDIT::event(int i)
  145. {
  146. WINDOW *w;
  147.     
  148.     w=app->front_window();
  149.  
  150.     if(w==NULL)
  151.         return;
  152.  
  153.     switch(i)
  154.         {
  155.         case 1:
  156.             w->cut();
  157.             break;
  158.  
  159.         case 2:
  160.              w->copy();
  161.              break;
  162.  
  163.         case 3:
  164.             w->paste();
  165.             break;
  166.         }
  167. }
  168.  
  169.  
  170. M_SUBMENU::M_SUBMENU(void)
  171. {
  172.     create(130,"");// Sub-menus may not have a name(thats how multiapp knows their sub)
  173.     add(000,"Sub Item 1",0);
  174.     add(000,"Sub Item 2",0);
  175.     add(000,"Sub Item 3",0);
  176.     add(000,"-",0);
  177.     add(000,"Sub Item A",0);
  178. }
  179.  
  180.  
  181. #pragma argsused
  182. void M_SUBMENU::event(int i){}
  183.  
  184.  
  185. void debug(char *format,...)
  186. {
  187. FILE *fp;
  188. char s[80];
  189. time_t now;
  190. struct tm *date;
  191. char string[200];
  192. va_list ptr;
  193.  
  194.     va_start(ptr,format);
  195.     vsprintf(string,format,ptr);
  196.     
  197.     now=time(NULL);
  198.     date=localtime(&now);
  199.     strcpy(s,asctime(date));
  200.     *(s+strlen(s)-1)=0;
  201.     fp=fopen("DEBUG.TXT","a");
  202.     if(fp==NULL)
  203.         fp=fopen("DEBUG.TXT","w");
  204.     
  205.     fprintf(fp,"%s : %s\r",s,string);
  206.     fclose(fp);
  207.     va_end(ptr);
  208. }
  209.  
  210.  
  211. class MULTI_APP:public WINDOW
  212. {
  213.     public:
  214.         int c;
  215.         MULTI_APP();
  216.         void update(void);
  217.         void idle(void);
  218. };
  219.  
  220.  
  221. MULTI_APP::MULTI_APP()
  222. {
  223.     create(128);
  224.     c=0;
  225.     set_title("MultiApp V1.0");
  226. }
  227.  
  228. void MULTI_APP::update(void)
  229. {
  230.  
  231.     goxy(0,0);
  232.     setcolor(COLORS[c]);
  233.     size(20);
  234.     center("MultiApp!");  
  235. }
  236.  
  237. void MULTI_APP::idle(void)
  238. {
  239. static long tm=0;
  240.  
  241.     if(!tm)
  242.         {
  243.         tm=timer_set(10);
  244.         }
  245.     if(timer_check(tm))
  246.         {
  247.         c++;
  248.         if(c==15)
  249.             c=0;
  250.         tm=timer_set(10);
  251.         update();
  252.         }
  253. }
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260. class CONNECT_DOTS:public WINDOW
  261. {
  262.     public:
  263.         CONNECT_DOTS();
  264.         void update(void);
  265.         void content(EVENT *e);
  266.         
  267.         int points[30][2];
  268.         int num_points;
  269. };
  270.  
  271. CONNECT_DOTS::CONNECT_DOTS()
  272. {
  273.     num_points=0;
  274.     create(129);
  275.     add(new V_SIZE);
  276.     set_title("Click Under Me!");
  277. }
  278.  
  279. void CONNECT_DOTS::update(void)
  280. {
  281. int x,y;
  282.  
  283.     for(x=0;x<num_points;x++)
  284.         {
  285.         setcolor(COLORS[rand()%15]);
  286.         for(y=0;y<num_points;y++)
  287.             {
  288.             
  289.             line(points[x][0],points[x][1],
  290.                  points[y][0],points[y][1],
  291.                  1);
  292.             }
  293.         }
  294.     WINDOW::update();
  295. }
  296.  
  297. void CONNECT_DOTS::content(EVENT *e)
  298. {
  299.     if(num_points==29)
  300.         {
  301.         beep();
  302.         return;
  303.         }
  304.     points[num_points][0]=e->x;
  305.     points[num_points++][1]=e->y;
  306.     redraw();
  307. }
  308.  
  309.  
  310. class CALC:public DLOG
  311. {
  312.     public:
  313.         CALC();
  314.         void keydown(char key);
  315.         void total(void);
  316.         void dlog(int i);
  317.         
  318.         char opp;
  319.         char str[80];
  320.         char hold[80];
  321.         int clr;
  322. };
  323.  
  324. CALC::CALC()
  325. {
  326.     create(128);
  327.     set_title("Calculator");
  328.     strcpy(str,"");
  329.     clr=0;
  330. }
  331.  
  332. void CALC::total(void)
  333. {
  334. double x,y,z;
  335. char *end;
  336.  
  337.     x=strtod(hold,&end);
  338.     y=strtod(str,&end);
  339.     switch(opp)
  340.         {
  341.         case '+':z=x+y;break;
  342.         case '-':z=x-y;break;
  343.         case '/':z=x/y;break;
  344.         case '*':z=x*y;break;
  345.         }
  346.     sprintf(str,"%10.lf",z);
  347.     clr=1;
  348. }
  349.     
  350.  
  351. void CALC::keydown(char key) 
  352. {
  353. char ky[2];
  354. double d;
  355. char *end;
  356. char s[80];
  357.  
  358.     if(key==0)
  359.         return;
  360.  
  361.     ky[1]=0;
  362.     if(isdigit(key))
  363.         {
  364.         if(clr)
  365.             {
  366.             *str=0;
  367.             clr=0;
  368.             }
  369.         if(strlen(str)>=9)
  370.             beep();
  371.         else{
  372.             ky[0]=key;
  373.         
  374.             strcat(str,ky);
  375.             }
  376.         }
  377.         
  378.     if(key=='.')
  379.         {
  380.         if(strchr(str,'.')!=NULL)
  381.             {
  382.             beep();
  383.             }
  384.         else{
  385.             ky[0]=key;
  386.             strcat(str,ky);
  387.             }
  388.         }
  389.     
  390.     switch(key)
  391.         {
  392.         case 'C':
  393.             strcpy(str,"");
  394.             strcpy(hold,"");
  395.             break;
  396.         case '+':
  397.         case '-':
  398.         case '/':
  399.         case '*':
  400.             opp=key;
  401.             strcpy(hold,str);
  402.             *str=0;
  403.             break;
  404.             
  405.         case '=':
  406.             total();
  407.             break;    
  408.         }
  409.         
  410.     d=strtod(str,&end);
  411.     sprintf(s,"%10.lf",d);
  412.     iset_text(10,s);
  413. }
  414.  
  415. void CALC::dlog(int i)
  416. {
  417.     if(i!=10)
  418.         {
  419.         char str[80];
  420.         
  421.         iget_text(i,str);
  422.         keydown(*str);
  423.         keydown(*(str+1));
  424.         }
  425. }
  426.  
  427. class EDITOR:public WINDOW
  428. {
  429.     public:
  430.     EDITOR();
  431.     void set(char *str);
  432.     void grow(EVENT *e);
  433.     V_TEXT *text;
  434. };
  435.  
  436. EDITOR::EDITOR()
  437. {
  438.     create(131);
  439.     text=new V_TEXT;
  440.     text->cV_TEXT(3,3,maxx-SCROLL_WIDTH,maxy-SCROLL_WIDTH);
  441.     add(focus=text);
  442.     add(new V_SIZE);
  443.     text->attach_scroll(1);
  444.     text->normal_position();
  445. }
  446.  
  447. void EDITOR::set(char *str)
  448. {
  449.     text->set_text(str);
  450. }
  451.  
  452. void EDITOR::grow(EVENT *e)
  453. {
  454.     VIEW::grow(e);
  455.     text->normal_position();
  456. }
  457.  
  458.  
  459. #ifdef msWINDOWS
  460. #pragma argsused
  461. int PASCAL WinMain( HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow)
  462. #else
  463. void main(void)
  464. #endif
  465. {
  466. EDITOR *v;
  467. V_TEXT *tex;
  468. V_GIF *gif;
  469.  
  470.     #ifdef MAC
  471.     app=new APP;
  472.     #endif
  473.  
  474.     #ifdef msWINDOWS
  475.     app=new APP(hInstance,hPrevInstance,nCmdShow);
  476.     #endif
  477.  
  478.  
  479.     app->play(128);
  480.     app->about_alert=129;
  481.     strcpy(app->about_menu,"About MultiApp...");
  482.  
  483.     app->add_menu(sub=new M_SUBMENU);
  484.     app->add_menu(file=new M_FILE);
  485.     app->add_menu(edit=new M_EDIT);
  486.     app->build_menu_bar();
  487.     update_menus();
  488.  
  489.     app->alert(128);
  490.     global_quit=0;
  491.  
  492.     app->add(new CALC);
  493.     app->add(new CONNECT_DOTS);
  494.     app->add(v=new EDITOR);
  495.     v->set("HELLO WORLD!!");
  496.  
  497.     app->add(new MULTI_APP);
  498.  
  499.  
  500.  
  501.     while(!global_quit)
  502.         {
  503.         app->process_event();
  504.         }
  505. }